package per.chenxin.scale;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;

public class ProxyDialog extends Dialog {

    protected Object proxy;

    protected Proxy oldProxy;

    protected Shell shlProxyDialog;

    private Text textInternalProxyHost;

    private Text textInternalProxyPort;

    private Text textUsername;

    private Text textPassword;

    private Button btnInternalProxy;

    private Button btnExternalProxy;

    private Button btnAuthcation;

    private Text textExternalProxyHost;

    private Text textExternalProxyPort;

    private Button btnok;

    private Button btncancle;

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public ProxyDialog(Shell parent, int style) {

        super(parent, SWT.DIALOG_TRIM);
        setText("SWT Dialog");
    }

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public ProxyDialog(Shell parent, int style, Proxy proxy) {

        this(parent, style);
        oldProxy = proxy;

    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {

        createContents();
        shlProxyDialog.open();
        shlProxyDialog.layout();
        Display display = getParent().getDisplay();
        while (!shlProxyDialog.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return proxy;
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {

        shlProxyDialog = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
        shlProxyDialog.setSize(320, 160);
        Point point = getParent().getLocation();
        if (point.x < 0)
            point.x = 0;
        if (point.y < 0)
            point.y = 0;
        shlProxyDialog.setLocation(point);
        shlProxyDialog.setText("Proxy Dialog");
        shlProxyDialog.setLayout(new FillLayout(SWT.HORIZONTAL));

        Composite composite = new Composite(shlProxyDialog, SWT.NONE);

        textInternalProxyHost = new Text(composite, SWT.BORDER);
        textInternalProxyHost.setEnabled(false);
        textInternalProxyHost.setToolTipText("Internal Proxy Host");
        textInternalProxyHost.setBounds(34, 10, 209, 18);

        textInternalProxyPort = new Text(composite, SWT.BORDER);
        textInternalProxyPort.setToolTipText("Internal Proxy Port");
        textInternalProxyPort.setEnabled(false);
        textInternalProxyPort.setBounds(249, 10, 54, 18);

        textUsername = new Text(composite, SWT.BORDER);
        textUsername.setToolTipText("username");
        textUsername.setEnabled(false);
        textUsername.setBounds(34, 34, 142, 18);

        textPassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
        textPassword.setToolTipText("password");
        textPassword.setEnabled(false);
        textPassword.setBounds(182, 34, 121, 18);

        btnAuthcation = new Button(composite, SWT.CHECK);
        btnAuthcation.setToolTipText("use authcation?");

        btnAuthcation.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                procAuthcation();
            }

        });
        btnAuthcation.setEnabled(false);
        btnAuthcation.setBounds(10, 34, 13, 16);

        btnExternalProxy = new Button(composite, SWT.CHECK);
        btnExternalProxy.setToolTipText("use external proxy?");
        btnExternalProxy.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                procExternal();
            }
        });
        btnExternalProxy.setEnabled(false);
        btnExternalProxy.setBounds(10, 59, 13, 16);

        btnInternalProxy = new Button(composite, SWT.CHECK);
        btnInternalProxy.setToolTipText("use proxy?");
        btnInternalProxy.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                procInternal();
                procAuthcation();
                procExternal();
            }
        });
        btnInternalProxy.setEnabled(true);
        btnInternalProxy.setSelection(oldProxy.isUseInternalProxy());

        btnInternalProxy.setBounds(10, 10, 13, 16);

        textExternalProxyHost = new Text(composite, SWT.BORDER);
        textExternalProxyHost.setToolTipText("External Proxy Host");
        textExternalProxyHost.setEnabled(false);
        textExternalProxyHost.setBounds(34, 58, 209, 18);

        textExternalProxyPort = new Text(composite, SWT.BORDER);
        textExternalProxyPort.setToolTipText("External Proxy Port");
        textExternalProxyPort.setEnabled(false);
        textExternalProxyPort.setBounds(249, 58, 54, 18);

        btnok = new Button(composite, SWT.NONE);
        btnok.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                boolean isOK = true;
                Proxy proxyTemp = Proxy.getNewProxy();

                ((Proxy) proxyTemp).setUseInternalProxy(btnInternalProxy.getSelection());

                if (((Proxy) proxyTemp).isUseInternalProxy()) {
                    String strTemp;
                    Integer intTemp = 0;

                    strTemp = checkStringText(textInternalProxyHost);
                    if (strTemp != null)
                        ((Proxy) proxyTemp).setInternalProxyHost(strTemp);
                    else
                        isOK = false;

                    intTemp = checkIntText(textInternalProxyPort);
                    if (intTemp != null)
                        ((Proxy) proxyTemp).setInternalProxyPort(intTemp);
                    else
                        isOK = false;

                    ((Proxy) proxyTemp).setAuthcation(btnAuthcation.getSelection());
                    if (((Proxy) proxyTemp).isAuthcation()) {

                        strTemp = checkStringText(textUsername);
                        if (strTemp != null)
                            ((Proxy) proxyTemp).setUsername(strTemp);
                        else
                            isOK = false;

                        strTemp = checkStringText(textPassword);
                        if (strTemp != null)
                            ((Proxy) proxyTemp).setPassword(strTemp);
                        else
                            isOK = false;

                    }

                    ((Proxy) proxyTemp).setUseExternalProxy(btnExternalProxy.getSelection());
                    if (((Proxy) proxyTemp).isUseExternalProxy()) {

                        strTemp = checkStringText(textExternalProxyHost);
                        if (strTemp != null)
                            ((Proxy) proxyTemp).setExternalProxyHost(strTemp);
                        else
                            isOK = false;

                        intTemp = checkIntText(textExternalProxyPort);
                        if (intTemp != null)
                            ((Proxy) proxyTemp).setExternalProxyPort(intTemp);
                        else
                            isOK = false;

                    }
                }

                if (isOK) {
                    proxy = proxyTemp;
                    shlProxyDialog.dispose();
                }

            }
        });
        btnok.setBounds(86, 96, 72, 22);
        btnok.setText("&OK");

        btncancle = new Button(composite, SWT.NONE);
        btncancle.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                proxy = null;
                shlProxyDialog.dispose();
            }
        });
        btncancle.setBounds(164, 96, 72, 22);
        btncancle.setText("&Cancle");

        initByProxy();

    }

    private void procInternal() {

        if (btnInternalProxy.isEnabled() && btnInternalProxy.getSelection()) {
            textInternalProxyHost.setEnabled(true);
            textInternalProxyPort.setEnabled(true);
            btnAuthcation.setEnabled(true);
            btnExternalProxy.setEnabled(true);

        } else {
            textInternalProxyHost.setEnabled(false);
            textInternalProxyPort.setEnabled(false);
            btnAuthcation.setEnabled(false);
            btnExternalProxy.setEnabled(false);

        }
    }

    private void procAuthcation() {

        if (btnAuthcation.isEnabled() && btnAuthcation.getSelection()) {
            textUsername.setEnabled(true);
            textPassword.setEnabled(true);
        } else {
            textUsername.setEnabled(false);
            textPassword.setEnabled(false);
        }
    }

    private void procExternal() {

        if (btnExternalProxy.isEnabled() && btnExternalProxy.getSelection()) {
            textExternalProxyHost.setEnabled(true);
            textExternalProxyPort.setEnabled(true);
        } else {
            textExternalProxyHost.setEnabled(false);
            textExternalProxyPort.setEnabled(false);
        }
    }

    private String checkStringText(Text text) {

        String str = text.getText();
        if (str != null && str.trim().length() > 0) {
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
            return str;
        }

        else {
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
            return null;
        }

    }

    private Integer checkIntText(Text text) {

        String str = text.getText();
        try {
            Integer intTemp = Integer.parseInt(str);
            if (intTemp <= 0)
                throw new NumberFormatException(" <= 0");
            else if (intTemp > 65535)
                throw new NumberFormatException(" > 65535 ");
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
            return intTemp;
        } catch (NumberFormatException nfe) {
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
            return null;
        }
    }

    private void initByProxy() {

        btnInternalProxy.setSelection(oldProxy.isUseInternalProxy());
        textInternalProxyHost.setText(oldProxy.getInternalProxyHost());
        textInternalProxyPort.setText(Integer.toString(oldProxy.getInternalProxyPort()));
        procInternal();

        btnAuthcation.setSelection(oldProxy.isAuthcation());
        textUsername.setText(oldProxy.getUsername());
        textPassword.setText(oldProxy.getPassword());
        procAuthcation();

        btnExternalProxy.setSelection(oldProxy.isUseExternalProxy());
        textExternalProxyHost.setText(oldProxy.getExternalProxyHost());
        textExternalProxyPort.setText(Integer.toString(oldProxy.getExternalProxyPort()));
        procExternal();

    }
}
